import os
import requests
from lxml import etree
def downloadimg():
    # dir=listurl[0].split('/')[-2]
    url='https://img.betaapi.com/2020/05/23/9c79d5952923a33208a893af34c07380.jpg'
    dir = "k:/girls/" +'naixue' + "/"

    if not os.path.exists(dir):  ##如果没有这个文件夹的话，创造并进入
        os.makedirs(dir)  ##创建一

    # for url in urls:
    dowloadheaders = {
        "accept": "image/webp,image/apng,image/*,*/*;q=0.8",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "cookie": 'cf_chl_1=a691491ac055bf6',
        "pragma": "no-cache",
        "referer":'https://www.naixue.org/45228.html',
        'sec-fetch-dest': 'image',
        "sec-fetch-mode": "no-cors",
        "sec-fetch-site": "cross-site",
        'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
    }
    ##上面美图录喜欢修改host
    # url="https://img.onvshen.com:85/gallery/23100/32147/s/001.jpg";
    response = requests.get(url, headers=dowloadheaders, allow_redirects=False)
    imgname = url.split('/')[-1]
    path = dir + imgname
    n = len(response.content)
    # 防止下载空文件
    if n > 1024:
        with open(path, 'wb')as img:
            print("Download: " + path)
            img.write(response.content)
downloadimg()